package library.common.util;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.BuildConfig;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

/**
 * @author sky_dreaming on 2017/3/25.
 */
public class LogUtils {
    private static String LOG_TAG = "sky_dreaming";
    /**
     * Whether to show thread info or not. Default true
     */
    private static boolean IS_SHOW_THREAD = true;
    /**
     * How many method line to show. Default 2
     */
    private static int METHOD_COUNT = 3;
    /**
     * Hides internal method calls up to offset. Default 5
     */
    private static int METHOD_OFFSET = 3;

    static {
        init();
    }

    private static void init() {
        Logger.clearLogAdapters();
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(IS_SHOW_THREAD)
                .methodCount(METHOD_COUNT)
                .methodOffset(METHOD_OFFSET)
                .tag(LOG_TAG)
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });
    }

    public static void v(String msg) {
        Logger.v(msg);
    }

    public static void v(String tag, String msg) {
        Logger.v(tag, msg);
    }

    public static void i(String msg) {
        Logger.i(msg);
    }

    public static void i(String tag, String msg) {
        Logger.i(tag, msg);
    }

    public static void d(String msg) {
        Logger.d(msg);
    }

    public static void d(String tag, String msg) {
        Logger.d(tag, msg);
    }

    public static void e(String error) {
        Logger.e(error);
    }

    public static void e(String tag, String error) {
        Logger.e(tag, error);
    }

    public static void e(String msg, Throwable throwable) {
        Logger.e(throwable, msg);
    }

    public static void json(String msg) {
        Logger.json(msg);
    }

    public static void xml(String msg) {
        Logger.xml(msg);
    }

    public static void setTag(String logTag) {
        LOG_TAG = logTag;
        init();
    }

    public static void setIsShowThreadInfo(boolean isShowThread) {
        IS_SHOW_THREAD = isShowThread;
        init();
    }

    public static void setMethodCount(int methodCount) {
        METHOD_COUNT = methodCount;
        init();
    }

    public static void setMethodOffset(int methodOffset) {
        METHOD_OFFSET = methodOffset;
        init();
    }
}
